package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {



    @Override
    public List<AddressBookResDTO> getByUserIdAndCity(Long userId, String city) {
        List<AddressBook> addressBooks = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getCity, city)
                .list();
        if (CollUtils.isEmpty(addressBooks)) {
            return new ArrayList<>();
        }
        return BeanUtils.copyToList(addressBooks, AddressBookResDTO.class);
    }

    /**
     * 新增地址
     *
     * @param dto 地址信息
     */
    @Override
    @Transactional
    public void add(AddressBookUpsertReqDTO dto) {
        if (ObjectUtil.isEmpty(dto)) {
            throw new ForbiddenOperationException("请求为空");
        }

        AddressBook addressBook = BeanUtils.copyBean(dto, AddressBook.class);// 将dto对象拷贝到addressBook对象中
        Long userId = UserContext.currentUserId();
        addressBook.setUserId(userId);
        String[] location = dto.getLocation().split(",");
        addressBook.setLon(Double.valueOf(location[0]));
        addressBook.setLat(Double.valueOf(location[1]));

        if (dto.getIsDefault() == 1) {
            lambdaUpdate()
                    .eq(AddressBook::getUserId, userId)
                    .set(AddressBook::getIsDefault, 0)
                    .update();
        }
        save(addressBook);
    }

    @Override
    public PageResult<AddressBookResDTO> getpage(AddressBookPageQueryReqDTO dto) {
        Page<AddressBook> page = PageUtils.parsePageQuery(dto, AddressBook.class);
        this.page(page, Wrappers.lambdaQuery(AddressBook.class).eq(AddressBook::getUserId, UserContext.currentUserId()));
        return PageUtils.toPage(page, AddressBookResDTO.class);
    }

    @Override
    @Transactional
    public void updateById(Long id, AddressBookUpsertReqDTO dto) {

        AddressBook addressBook = getById(id);
        if (ObjectUtil.isEmpty(addressBook)) {
            throw new ForbiddenOperationException("地址不存在");
        }
        AddressBook now = BeanUtils.copyBean(dto, AddressBook.class);
        BeanUtils.copyProperties(now, addressBook, CopyOptions.create(null, true));
        updateById(addressBook);
    }

    @Override
    public void setDefault(Long id, int flag) {
        lambdaUpdate()
                .eq(AddressBook::getUserId, UserContext.currentUserId())
                .eq(AddressBook::getIsDefault, 1)
                .set(AddressBook::getIsDefault, 0)
                .update();
        lambdaUpdate()
                .eq(AddressBook::getId, id)
                .set(AddressBook::getIsDefault, flag)
                .update();
    }

    /**
     * 获取默认地址
     *
     * @return
     */

    @Override
    public AddressBookResDTO getDefaultAddress() {
        AddressBook addressBook = lambdaQuery()
                .eq(AddressBook::getUserId, UserContext.currentUserId())
                .eq(AddressBook::getIsDefault, 1)
                .one();
        if (ObjectUtil.isNotEmpty(addressBook)) {
            return BeanUtils.copyBean(addressBook, AddressBookResDTO.class);
        }
        return null;
    }
}